package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javabean.Admin;
import javabean.Base;
import javabean.JDBCBean;
import javabean.Util;
import net.sf.json.JSONObject;


@WebServlet("/admin/orderassign")
public class OrderAssign extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");
		HttpSession session = req.getSession();
		String id = req.getParameter("id");
		String oid = req.getParameter("oid");
		String area = null;
		String nextstep = null;
		String state =null;
		Admin admin = new Admin();
		try {
			area = admin.area(session.getAttribute("admin").toString());
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		JSONObject json = new JSONObject();
		JDBCBean db = new JDBCBean();
		String sql = "select * from "+area+"order where Oid = "+oid;
		ResultSet resultset = db.executeQuery(sql);
		try {
			if(resultset.next()){
				String nx= resultset.getString("nextstep");
				sql ="select * from oorder where Oid ="+oid;
				resultset = db.executeQuery(sql);
				resultset.next();
				if(resultset.getString("nowarea").equals(resultset.getString("destination"))){
					nextstep = resultset.getString("nowarea");
				}else {
					nextstep = nx;
				}
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		String sql1 = "insert into `"+id+"delivery`(`Oid`,`destination`,`nowarea`,`state`) value("+oid+",'"+nextstep+"','"+area+"','0')";

		int result = 0;
		int code = 1;
		String msg = "";
		if( oid!=null&& nextstep!=null&& id != null && !id.equals("") ) {
			result = db.executeUpdate(sql1);
		}
		if( result == 1 ) {
			code = 0;
			msg = "分配成功等待接单";
		}else {
			code = 1;
			msg = "已分配";
		}
		json.put("code", code);
		json.put("msg", msg);
//		db.close();
		PrintWriter out = resp.getWriter();
		out.print( json.toString() );

	}

}
//		resp.setContentType("application/json; charset=utf8");
//		// 接受数据
//		String id = req.getParameter("id");
//		System.out.println(id);
//		String name = req.getParameter("name");
//		String description = req.getParameter("description");
		
		// 准备数据
//		Connection connection = null;
//		PreparedStatement pstmt = null;
//		ResultSet resultSet = null;
//		int result = 0;
//		String sql = "";
//		int count = 0 ;
		// 准备返回数据
//		int code = 1;
//		String msg = "123";
//		JSONObject json = new JSONObject();
		
//		try {
//			connection = (Connection) Base.getConnection();
//			// 查询重复name
//			sql = "select count(*) as count from book_sort where name=? and id != ?";
//			pstmt = connection.prepareStatement(sql);
//			pstmt.setString(1, name);
//			pstmt.setString(2, id);
//			resultSet = pstmt.executeQuery();
//			if(resultSet.next()) {
//				// 有重复
//				if(resultSet.getInt("count") > 0) {
//					msg = "分类名不能重复";
//				}else {
//					// 进行插入
//					sql = "update book_sort set name=?, description=? where id=?";
//					pstmt = connection.prepareStatement(sql);
//					pstmt.setString(1, name);
//					pstmt.setString(2, description);
//					pstmt.setString(3, id);
//					result = pstmt.executeUpdate();
//					if(result == 1) {
//						code = 0;
//						msg = "修改成功";
//					}else {
//						msg = "修改失败";
//					}
//				}
//			}
//
//		} catch (ClassNotFoundException e) {
//			msg = "classnotfound";
//		} catch (SQLException e) {
//			msg = "SQL错误";
//		} finally {
//			try {
//				Base.closeResource(connection, pstmt, resultSet);
//			} catch (SQLException e) {
//				msg = "关闭失败";
//			}
//		}
//		json.put("code", code);
//		json.put("msg", msg);
//		PrintWriter out = resp.getWriter();
//		out.print( json.toString() );
//
//	}
//}
